package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.http.HttpAdapter;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class MessageAdapter extends BaseListAdapter<LinkedTreeMap> {

    public MessageAdapter(Context context, List<LinkedTreeMap> objects) {
        super(context, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_message_item, parent, false);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.content = (TextView) convertView.findViewById(R.id.content);
                holder.time = (TextView) convertView.findViewById(R.id.time);
                holder.nameHf01 = (TextView) convertView.findViewById(R.id.nameHf01);
                holder.contentHf01 = (TextView) convertView.findViewById(R.id.contentHf01);
                holder.nameHf02 = (TextView) convertView.findViewById(R.id.nameHf02);
                holder.contentHf02 = (TextView) convertView.findViewById(R.id.contentHf02);
                holder.nameHf03 = (TextView) convertView.findViewById(R.id.nameHf03);
                holder.contentHf03 = (TextView) convertView.findViewById(R.id.contentHf03);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.hfLay = (LinearLayout) convertView.findViewById(R.id.hfLay);
                holder.hf01 = (LinearLayout) convertView.findViewById(R.id.hf01);
                holder.hf02 = (LinearLayout) convertView.findViewById(R.id.hf02);
                holder.hf03 = (LinearLayout) convertView.findViewById(R.id.hf03);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(),getScreenHeight()*2/3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            holder.content.setText(mDataList.get(position).get("book_content") + "");
            holder.time.setText(mDataList.get(position).get("book_date") + "");
            List<LinkedTreeMap> linkedTreeMaps = (List<LinkedTreeMap>) mDataList.get(position).get("book_reply");
            if (TextUtils.isEmpty(mDataList.get(position).get("user_name") + "")){
                holder.name.setText("匿名");
            }else{
                holder.name.setText(mDataList.get(position).get("user_name") + "");
            }
            if (linkedTreeMaps.size() == 0) {
                holder.hfLay.setVisibility(View.GONE);
            }else if (linkedTreeMaps.size() == 1) {
                holder.hfLay.setVisibility(View.VISIBLE);
                holder.hf01.setVisibility(View.VISIBLE);
                holder.hf02.setVisibility(View.GONE);
                holder.hf03.setVisibility(View.GONE);
                holder.contentHf01.setText(linkedTreeMaps.get(0).get("book_content")+"");
                if (TextUtils.isEmpty(linkedTreeMaps.get(0).get("user_name") + "")){
                    holder.nameHf01.setText("匿名:");
                }else{
                    holder.nameHf01.setText(linkedTreeMaps.get(0).get("user_name")+":");
                }
            }else if (linkedTreeMaps.size() == 2) {
                holder.hfLay.setVisibility(View.VISIBLE);
                holder.hf01.setVisibility(View.VISIBLE);
                holder.hf02.setVisibility(View.VISIBLE);
                holder.hf03.setVisibility(View.GONE);
                if (TextUtils.isEmpty(linkedTreeMaps.get(0).get("user_name") + "")){
                    holder.nameHf01.setText("匿名:");
                }else{
                    holder.nameHf01.setText(linkedTreeMaps.get(0).get("user_name")+":");
                }
                holder.contentHf01.setText(linkedTreeMaps.get(0).get("book_content")+"");
                if (TextUtils.isEmpty(linkedTreeMaps.get(1).get("user_name") + "")){
                    holder.nameHf02.setText("匿名:");
                }else{
                    holder.nameHf02.setText(linkedTreeMaps.get(1).get("user_name")+":");
                }
                holder.contentHf02.setText(linkedTreeMaps.get(1).get("book_content")+"");
            }else if (linkedTreeMaps.size() == 3) {
                holder.hfLay.setVisibility(View.VISIBLE);
                holder.hf01.setVisibility(View.VISIBLE);
                holder.hf02.setVisibility(View.VISIBLE);
                holder.hf03.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(linkedTreeMaps.get(0).get("user_name") + "")){
                    holder.nameHf01.setText("匿名:");
                }else{
                    holder.nameHf01.setText(linkedTreeMaps.get(0).get("user_name")+":");
                }
                holder.contentHf01.setText(linkedTreeMaps.get(0).get("book_content")+"");
                if (TextUtils.isEmpty(linkedTreeMaps.get(1).get("user_name") + "")){
                    holder.nameHf02.setText("匿名:");
                }else{
                    holder.nameHf02.setText(linkedTreeMaps.get(1).get("user_name")+":");
                }
                holder.contentHf02.setText(linkedTreeMaps.get(1).get("book_content")+"");
                if (TextUtils.isEmpty(linkedTreeMaps.get(2).get("user_name") + "")){
                    holder.nameHf03.setText("匿名:");
                }else{
                    holder.nameHf03.setText(linkedTreeMaps.get(2).get("user_name")+":");
                }
                holder.contentHf03.setText(linkedTreeMaps.get(2).get("book_content")+"");
            }
        }
        return convertView;
    }

    private static final class ViewHolder {
        TextView name, content, time, nameHf01,contentHf01, nameHf02,contentHf02, nameHf03,contentHf03;
        ImageView image;
        LinearLayout noDataRootLayout, hfLay, hf01, hf02, hf03;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

    public static String formatDate1(String dateStr) {
        DateFormat formatFrom = new SimpleDateFormat("MMM dd,yyyy KK:mm:ss aa", Locale.ENGLISH);
        Date date = null;
        try {
            date = formatFrom.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        DateFormat formatTo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatTo.format(date)+"";
    }
}
